/*
 * Copyright (c) 2011 Srdjan VESIC.
 * Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; GNU Free Documentation License is available at
http://www.gnu.org/copyleft/fdl.html
*/ 
	package map;
import java.awt.BorderLayout;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashSet;
import java.util.Hashtable;
import tests.Scenario1;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Display map of airport
 *@author NOMS DES MEMBRES DU GROUPE
 *@version 1.0
 */
public class GUI extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	/** Button load */ 
	private JButton buttonLoad;
	/** Button close */
	private JButton buttonClose;
	/** Button Scenario 1 */
	private JButton buttonScenario1;
	/** Our canvas for displaying the map */
	private PaintMap mc; 
	/** Label containing the name of the current map*/
	private JLabel nameOfMap; 

	/** Panel title */
	private JPanel titlePanel;
	/** Panel center */
	private JPanel centerPanel;
	/** Panel for button */
	private JPanel buttonPanel;
	/** Panel east of window */
	private JPanel eastPanel;

	/** Box to display options */
	private JComboBox comboBoxDisplay; 

	/** Set of airports */
	private Hashtable<String,Airport> airports; 

	private JFileChooser fc;

	public GUI() throws Exception {
		JPanel myPanel = new JPanel(new BorderLayout());
		fc = new JFileChooser();
		// Upper part which contains the title. 
		titlePanel = new JPanel();
		// Middle of the panel, containing the map.  
		centerPanel = new JPanel();
		// Lower part which contains buttons used for loading and closing a file.
		buttonPanel = new JPanel();
		// Right part containing display. 
		eastPanel = new JPanel(new GridLayout(15,1));

		// Add components:
		JLabel currentMap = new JLabel("Curently loaded map: ");
		titlePanel.add(currentMap);
		nameOfMap = new JLabel("<none>");
		titlePanel.add(nameOfMap);

		JLabel labelInfo = new JLabel("Display options:");

		String[] options = { "Cities and flights", "Cities only"};

		comboBoxDisplay = new JComboBox(options);
		comboBoxDisplay.setSelectedIndex(0);

		comboBoxDisplay.addActionListener(this);
		
		
		buttonScenario1 = new JButton("Scenario 1");
		buttonScenario1.setToolTipText("Launch Scenario 1");
		buttonScenario1.setEnabled(true); // Change to false after
		buttonScenario1.addActionListener(this);
		
		eastPanel.add(labelInfo);
		eastPanel.add(comboBoxDisplay);
		//Some empty space.
		eastPanel.add(new JLabel());
		eastPanel.add(buttonScenario1);

		buttonLoad = new JButton("Load...");
		buttonLoad.setToolTipText("Load a map from a file.");
		buttonLoad.addActionListener(this);

		buttonClose = new JButton("Close");
		buttonClose.setToolTipText("Close current map.");
		buttonClose.setEnabled(true); // Change to false after
		buttonClose.addActionListener(this);


		buttonPanel.add(buttonLoad);
		buttonPanel.add(buttonClose);

		// Create an empty set of airports.
		airports = new Hashtable<String, Airport>();
		
		// Create a canvas.
		mc = new PaintMap(airports);

		// The map has dimensions 600 x 600 pixels.
		mc.setSize(600, 600);

		// Add the map to the central panel.
		centerPanel.add(mc);

		myPanel.add(titlePanel, BorderLayout.NORTH);
		myPanel.add(centerPanel, BorderLayout.CENTER);
		myPanel.add(buttonPanel, BorderLayout.SOUTH);
		myPanel.add(eastPanel, BorderLayout.EAST);

		add(myPanel);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals(buttonLoad.getActionCommand())) {
			// The following code is executed when and only when button "Load" is clicked:

			// Open the file:
			int returnVal = fc.showOpenDialog(GUI.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				String fileName = file.toString();

				// Read the file and update the list of cities (in the object mc).
				mc.updateCitiesFromFile(fileName);

				// Repaint the map.
				mc.repaint();

				// Update the name of the map.
				nameOfMap.setText(fileName);

				// Enable / disable buttons.
				buttonClose.setEnabled(true);
				buttonLoad.setEnabled(false);
			} 
		}

		if (e.getActionCommand().equals(buttonClose.getActionCommand())) {
			// The following code is executed when and only when button "Close" is clicked:
			// Delete all airports and repaint the canvas:
			airports = new Hashtable<String, Airport>();
			mc.updateCitiesAirportList(airports);
			mc.repaint();

			// Update map name.
			nameOfMap.setText("<none>");

			// Enable / disable buttons.
			buttonClose.setEnabled(false);
			buttonLoad.setEnabled(true);
		}
		
		if(e.getActionCommand().equals(comboBoxDisplay.getActionCommand())) {
			if(comboBoxDisplay.getSelectedItem().toString().equals("Cities and flights")) {
				mc.setDisplayFlights(true);
			} else {
				mc.setDisplayFlights(false);
			}
			mc.repaint();
		}
		
		if(e.getActionCommand().equals(buttonScenario1.getActionCommand())) {
			Scenario1 sc = new Scenario1();
			sc.start();
		}
	} // end of function actionPerformed()

	/** Creates and sets up the window. */
	static private void createAndShowGUI() throws Exception {        
		JFrame frame = new JFrame("Map");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);

		//Create and set up the content pane.
		GUI newContentPane = new GUI();
		newContentPane.setOpaque(true); 
		frame.setContentPane(newContentPane);

		frame.setLocation(100, 10);

		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) throws Exception {
		createAndShowGUI();
	}
}



